--[[
机器人操作

--]]

local cjson = require("cjson")

local math_random = math.random
local math_floor = math.floor

local M = class("Oper")

function M:ctor()

    self.weiht_oper = new("games.texas.robot.weight_oper") -- 机器人权重操作方式
    self.cards_oper = new("games.texas.robot.cards_oper")  -- 机器人好牌概率操作方式
    self.rlcard_oper = new("games.texas.robot.ai_oper")  -- rlcard
    self.kill_oper = new("games.texas.robot.kill_oper")  -- 必杀
end

function M:register()

end

--下注
function M:betChip(user)
    if not user:isRobot() then return end
    g.fork(function()
        local uid = user:getUid()
        local chip = -1
        local rate = math_random(1, 100)
        if g.rid == 4 and rate < 10 then
            --在4号场测试rlcard
            --chip = self.rlcard_oper:getBetChip(user)
            --if chip == -2 then
            --    chip = self.weiht_oper:getBetChip(user)
            --end
            --必杀
            chip = self.kill_oper:getBetChip(user)
        else
            if g.desk:hasPlayer() then
                --有真人
                if g.rid <= 3  then
                    local rand_oper = math_random(1, 100)
                    --if g.desk:getStage() == g.desk.s_stage_preflop then
                    --    if rand_oper == 1 then
                    --        chip = self.weiht_oper:getBetChip(user)
                    --    else
                    --        chip = self.cards_oper:getBetChip(user)
                    --    end
                    --else
                    --    if rand_oper == 1 then
                    --        chip = self.weiht_oper:getBetChip(user)
                    --    else
                    --        chip = self.cards_oper:getBetChip(user)
                    --    end
                    --end

                    if rand_oper < 20 then
                        chip = self.cards_oper:getBetChip(user)
                    else
                        chip = self.weiht_oper:getBetChip(user)
                    end
                elseif g.desk.getUseSkill and g.desk:getUseSkill() then
                    chip = self.kill_oper:getBetChip(user)
                else
                    chip = self.weiht_oper:getBetChip(user)
                end

            else
                --无真人
                chip = self.cards_oper:getBetChip(user)
            end

        end


        --if g.desk:hasPlayer() then
        --    g.log:warn("rand_oper:", rand_oper)
        --end

        local max_time = 400
        local min_time = 100
        local rand_num = math_random(1,100)
        if rand_num < 10 then
            --有概率出现无时间思考
            max_time = 200
            min_time = 100
        elseif rand_num < 20 then
            --有概率出现长时间思考
            max_time = 800
            min_time = 300
        end
        if g.desk:isNormalRoom() then
            --普通场单位是分
            --if g.desk.sb and g.desk.sb >= 100 then
            --    --下注额度为100的整数
            --    if chip ~= user:getChip() and chip > 100 then
            --        chip = math_floor(chip / 100) * 100
            --    end
            --end
        end

        if chip == 0 or chip == -1 then
            --check 的话有概率快速操作
            if rand_num < 50 then
                if g.desk:getStage() ~= g.desk.s_stage_preflop then
                    min_time = 50
                    max_time = 200
                else
                    min_time = 150
                    max_time = 300
                end
            end
        end
        local sleep_time = math_random(min_time, max_time) / 100
        g.timeout(sleep_time, function()
            local msg = {
                mid = uid,
                tid = g.tid,
                chips = chip,
            }
            g.desk:onBet(uid, msg)
        end)

    end)
    --输出牌
    g.desk:printCards(user)

end

--结束时处理机器人
function M:gameOver(gusers)
    local function isRobot(mid)
        local user = g.user_mgr:getUser(mid)
        if user and user:isRobot() then
            return true
        end
        return false
    end
    local win_user = nil
    local lose_user = nil
    local is_robot = nil
    for _, user in pairs(gusers) do
        is_robot = isRobot(user.mid)
        if is_robot then
            if user.win > 0 then
                if math_random(1, 4) == 1 then
                    g.timeout(math_random(1, 3), function()
                        self:reqShowCard(user)
                    end)
                end
                win_user = user
            end
            if g.rid == 4 then
                --在4号场测试rlcard
                --self:postGameOver(user.mid, user.win)
            end
        end
        if user.win < 0 then
            lose_user = user
        end

    end
    if win_user and lose_user then
        local num = math_random(1, 15)
        if num == 1 then
            g.timeout(math_random(2, 4), function()
                self:reqMessage(win_user)
            end)
        elseif num == 2 then
            g.timeout(math_random(2, 4), function()
                self:reqMagicMsg(win_user, lose_user)
            end)
        elseif num == 3 then
            --g.timeout(math_random(2, 4), function()
            --    self:reqTextMsg(win_user)
            --end)
        end
    end
end

--发表情与信息
function M:reqMessage(win_user)
    --表情， 4，13， 23
    local tb = {
        4, 6, 13, 16, 23,
    }
    local msg = {
        type = "chat",
        emoji = tb[math_random(1,#tb)],
    }
    local data = {
        mid = win_user.mid,
        tid = g.tid,
        msg = cjson.encode(msg),
    }
    g.chandler:reqMessage(data)
end

function M:reqTextMsg(win_user)
    local tb = {
        "Thanks!", "Nice Play!", "I'am Lucky!",
    }
    local msg = {
        type = "chat",
        msg = tb[math_random(1,#tb)],
    }
    local data = {
        mid = win_user.mid,
        tid = g.tid,
        msg = cjson.encode(msg),
    }
    g.chandler:reqMessage(data)
end

--魔法表情
function M:reqMagicMsg(win_user, lose_user)
    local tb = {
        "rose", "beer"
    }
    local to_mid = lose_user.mid
    local msg = {
        type = "one_to_one",
        toMid = to_mid,
        key = tb[math_random(1,#tb)],
    }
    local data = {
        mid = win_user.mid,
        tid = g.tid,
        msg = cjson.encode(msg),
    }
    g.chandler:reqMessage(data)
end

function M:reqShowCard(guser)
    if guser.cards and next(guser.cards) then
        --已经是在最后阶段显示了牌
        return
    end
    local mid = guser.mid
    local user = g.user_mgr:getUser(mid)
    if not user then return end
    local data = {
        mid = mid,
        cards = user:getClientCards(),
        tid = g.tid,
    }
    g.chandler:reqShowCard(data)
end

function M:postGameOver(uid, reward)
    if g.desk:isMatchRoom() then
        return
    end
    local data = {
        uid = "".. uid,
        reward = reward
    }
    local res = g.message:httpPostJson("rlcard", "/game_over", data)
    if res.code ~= g.error_code.ok then
        g.log:err("httpPostJson rlcard err", res)
        return
    end
end

return M